package sjtu.ist.warehouse4ws.tool.parser;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.SAXException;

import sjtu.ist.warehouse4ws.model.Functionpoint;
import sjtu.ist.warehouse4ws.model.Parameter;
import sjtu.ist.warehouse4ws.tool.ParserFunctionpoint;

public class FunctionPointParser {
	private File file = null; // 模型文件
	private Document doc = null; // 文档对象
	private Functionpoint functionPoint = null;
	private String pointId;
	private ArrayList<Parameter> paramList = null;

	public FunctionPointParser(File file) {
		this.file = file;

	}

	public void parse() {
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = null;
		try {
			db = dbf.newDocumentBuilder();
			doc = db.parse(file);
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		functionPoint=new Functionpoint();
		resolvePoint();
		resolveParameter();
	}

	public String parseNode(String tagName, Document doc) {

		NodeList parentlist = doc.getElementsByTagName(tagName); // 获取该ID的标签

		Element head = (Element) parentlist.item(0); // 获取该ID标签的第一个Item

		NodeList childlist = head.getChildNodes(); // 获取该ID标签第一个Item里面的结点
		if (childlist.getLength() != 0) {
			String tagvalue = childlist.item(0).getNodeValue(); // 取值
			if (tagvalue.isEmpty()) {
				return null;
			} else {
				return tagvalue;
			}
		} else {
			return null;
		}

	}

	/**
	 * 解析功能点
	 */
	private void resolvePoint() {

		pointId = parseNode("id", doc);
		String enName = parseNode("enName", doc);
		String cnName = parseNode("cnName", doc);
		String description = parseNode("description", doc);

		functionPoint.setId(pointId);
		functionPoint.setEnName(enName);
		functionPoint.setCnName(cnName);
		functionPoint.setDescription(description);
		// funPoint.setFileinfo(null);

	}

	/**
	 * 解析功能点参数
	 */
	public void resolveParameter() {

		paramList = new ArrayList<Parameter>();

		Element root = doc.getDocumentElement();
		NodeList paramlist = root.getElementsByTagName("parameter");
		for (int i = 0; i < paramlist.getLength(); i++) {
			Parameter parameter = new Parameter();
			Element param = (Element) paramlist.item(i);

			NodeList idList = param.getElementsByTagName("id");
			String id = getNodeText(idList);
			parameter.setId(id);
			System.out.println(id);

			NodeList enNameList = param.getElementsByTagName("enName");
			String enName = getNodeText(enNameList);
			parameter.setEnName(enName);

			NodeList cnNameList = param.getElementsByTagName("cnName");
			String cnName = getNodeText(cnNameList);
			parameter.setCnName(cnName);

			NodeList descriptionList = param
					.getElementsByTagName("description");
			String description = getNodeText(descriptionList);
			parameter.setDescription(description);

			NodeList inOutTypeList = param.getElementsByTagName("inOutType");
			String inOutType = getNodeText(inOutTypeList);
			parameter.setInOutType(inOutType);

			NodeList isBuiltInList = param.getElementsByTagName("isBuiltIn");
			String isBuiltIn = getNodeText(isBuiltInList);
			if (isBuiltIn == "true") {
				parameter.setIsBuiltIn(true);
			}
			if (isBuiltIn == "false") {
				parameter.setIsBuiltIn(false);
			} else {
				parameter.setIsBuiltIn(null);
			}
			System.out.println(isBuiltIn);

			NodeList paramTypeNameList = param
					.getElementsByTagName("paramTypeName");
			String paramTypeName = getNodeText(paramTypeNameList);
			parameter.setParamTypeName(paramTypeName);

			NodeList paramTypeIdList = param
					.getElementsByTagName("paramTypeId");
			String paramTypeId = getNodeText(paramTypeIdList);
			parameter.setParamTypeId(paramTypeId);

			NodeList collectionTypeList = param
					.getElementsByTagName("collectionType");
			String collectionType = getNodeText(collectionTypeList);
			parameter.setCollectionType(collectionType);

			NodeList restrictionsList = param
					.getElementsByTagName("restrictions");
			String restrictions = getNodeText(restrictionsList);
			parameter.setRestrictions(restrictions);

			parameter.setFunctionPointId(pointId);
			paramList.add(parameter);
		}
	}

	public String getNodeText(NodeList node) {
		Element e = (Element) node.item(0);
		if (e == null) {
			return null;
		} // this judge is very important ,without it ,you will found nullpoint
			// Exception
		Text t = (Text) e.getFirstChild();
		if (t == null) {
			return null;
		}
		String str = t.getNodeValue();
		return str;

	}

	public static void main(String[] args) throws IOException, Exception {

		ParserFunctionpoint test = new ParserFunctionpoint();
		String path = "E:/Users/chenqidou/Desktop/functions/3a2a53ad-78da-46c0-b35b-95adfd70b66b.xml";
		test.reservePoint(path);
		ArrayList<Parameter> testparam = test.reserveParameter(path);
		for (int i = 0; i < testparam.size(); i++) {
			System.out.println(testparam.get(i).getDescription());
			System.out.println(testparam.get(i).getCnName());
			System.out.println(testparam.get(i).getEnName());
			System.out.println(testparam.get(i).getFunctionPointId());

		}
	}

	public Document getDoc() {
		return doc;
	}

	public Functionpoint getFunctionPoint() {
		return functionPoint;
	}

	public String getPointId() {
		return pointId;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public ArrayList<Parameter> getParamList() {
		return paramList;
	}
}
